<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="id" prop="id">
        <el-input v-model="dataForm.id" placeholder="id"></el-input>
      </el-form-item>
      <el-form-item label="申请人id" prop="applUserId">
        <el-input v-model="dataForm.applUserId" placeholder="申请人id"></el-input>
      </el-form-item>
      <el-form-item label="申请人名称" prop="applUser">
        <el-input v-model="dataForm.applUser" placeholder="申请人名称"></el-input>
      </el-form-item>
      <el-form-item label="申请时间" prop="applDate">
        <el-input v-model="dataForm.applDate" placeholder="申请时间"></el-input>
      </el-form-item>
      <el-form-item label="业务申请单id" prop="applId">
        <el-input v-model="dataForm.applId" placeholder="业务申请单id"></el-input>
      </el-form-item>
      <el-form-item label="业务申请单类型:0需求，1报价" prop="applType">
        <el-input v-model="dataForm.applType" placeholder="业务申请单类型:0需求，1报价"></el-input>
      </el-form-item>
      <el-form-item label="申请提交次数序号" prop="applSeqNum">
        <el-input v-model="dataForm.applSeqNum" placeholder="申请提交次数序号"></el-input>
      </el-form-item>
      <el-form-item label="审批状态 0:未提交 1：审批中 2：已完成 3：已撤销 4：已退回 5：已终止 6：通过后撤销" prop="aprvStatus">
        <el-input v-model="dataForm.aprvStatus" placeholder="审批状态 0:未提交 1：审批中 2：已完成 3：已撤销 4：已退回 5：已终止 6：通过后撤销"></el-input>
      </el-form-item>
      <el-form-item label="审批结果（最终）1：同意 2：拒绝 3：退回" prop="aprvEndResult">
        <el-input v-model="dataForm.aprvEndResult" placeholder="审批结果（最终）1：同意 2：拒绝 3：退回"></el-input>
      </el-form-item>
      <el-form-item label="审批人id（最终）" prop="aprvUserId">
        <el-input v-model="dataForm.aprvUserId" placeholder="审批人id（最终）"></el-input>
      </el-form-item>
      <el-form-item label="审批时间（最终）" prop="aprvEndDate">
        <el-input v-model="dataForm.aprvEndDate" placeholder="审批时间（最终）"></el-input>
      </el-form-item>
      <el-form-item label="审批人（最终）" prop="aprvUser">
        <el-input v-model="dataForm.aprvUser" placeholder="审批人（最终）"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateDate">
        <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="更新者" prop="updater">
        <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
      </el-form-item>
      <el-form-item label="逻辑删除 0：未删除 1：已删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="逻辑删除 0：未删除 1：已删除"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  applUserId: "",
  applUser: "",
  applDate: "",
  applId: "",
  applType: "",
  applSeqNum: "",
  aprvStatus: "",
  aprvEndResult: "",
  aprvUserId: "",
  aprvEndDate: "",
  aprvUser: "",
  createDate: "",
  creator: "",
  updateDate: "",
  updater: "",
  isDeleted: "",
  remark: "",
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/swfProcInst/swfprocinst/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/swfProcInst/swfprocinst", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>